unit moveroutestringizer;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpjson, database;

type

  { TMoveRouteStringizer }

  TMoveRouteStringizer = class
    public
      Db: TDatabase;
      function StringizeMoveCommand(MoveCommand: TJSONObject): String;
  end;

resourcestring
  rsMoveDown = 'Move down ↓';
  rsMoveLeft = 'Move left ←';
  rsMoveRight = 'Move right →';
  rsMoveUp = 'Move up ↑';
  rsMoveLowerLeft = 'Move lower left ↙';
  rsMoveLowerRight = 'Move lower right ↘ ';
  rsMoveUpperLeft = 'Move upper left ↖';
  rsMoveUpperRight = 'Move upper right ↗';
  rsMoveAtRandom = 'Move at random';
  rsMoveTowardsPlayer = 'Move towards player';
  rsMoveAwayFromPlayer = 'Move away from player';
  rs1StepForward = '1 step forward';
  rs1StepBackward = '1 step backward';
  rsJump = 'Jump: %s, %s';
  rsWait = 'Wait: %d frame(s)';

  rsTurnDown = 'Turn down ↓';
  rsTurnLeft = 'Turn left ←';
  rsTurnRight = 'Turn right →';
  rsTurnUp = 'Turn up ↑';
  rsTurn90DegreesRight = 'Turn 90° right';
  rsTurn90DegreesLeft = 'Turn 90° left';
  rsTurn180Degrees = 'Turn 180° right';
  rsTurn90DegreesRightOrLeft = 'Turn 90° right or left';

  rsTurnAtRandom = 'Turn at random';
  rsTurnTowardsPlayer = 'Turn towards player';
  rsTurnAwayFromPlayer = 'Turn away from player';
  rsSwitchOn = 'Switch ON: %s';
  rsSwitchOff = 'Switch OFF: %s';
  rsSpeed = 'Speed: %d';
  rsFrequency = 'Frequency: %d';

  rsWalkingAnimationOn = 'Walking animation ON';
  rsWalkingAnimationOff = 'Walking animation OFF';
  rsSteppingAnimationOn = 'Stepping animation ON';
  rsSteppingAnimationOff = 'Stepping animation OFF';
  rsDirectionFixOn = 'Direction fix ON';
  rsDirectionFixOff = 'Direction fix OFF';
  rsThroughOn = 'Pass-through ON';
  rsThroughOff = 'Pass-through OFF';
  rsTransparentOn = 'Transparency ON';
  rsTransparentOff = 'Transparency OFF';
  rsImageNone = 'Image: None';
  rsImage = 'Image: %s(%d)';
  rsOpacity = 'Opacity: %d';
  rsBlendMode = 'Blend mode: %s';
  rsPlaySe = 'Play SE: %s (%d, %d, %d)';

  rsScript = 'Script: %s';

implementation

uses
  ecstringiser, constants, namehelper;

{ TMoveRouteStringizer }

function TMoveRouteStringizer.StringizeMoveCommand(MoveCommand:
  TJSONObject): String;
var
  Code: Integer = 0;
  CodeJson: TJSONNumber;

  Params: TJSONArray = nil;

  function ParamWithSign(ParamId: Integer): String;
  var
    Value: Integer;
  begin
    ParamWithSign := '';

    if (Params <> nil) and (ParamId >= 0) and (ParamId < Params.Count) then begin
      Value := Params.Integers[ParamId];
      if Value < 0 then
        ParamWithSign := IntToStr(Value)
      else
        ParamWithSign := '+' + IntToStr(Value);
    end;
  end;

  function Image: String;
  var
    ImageFile: String = '';
    ImageIndex: Integer = 0;
  begin
    ImageFile := Params.Strings[0];
    ImageIndex := Params.Integers[1];

    if (ImageFile = '') then
      Image := rsImageNone
    else
      Image := rsImage.Format([ImageFile, ImageIndex]);
  end;

  function BlendMode: String;
  begin
    BlendMode := GetBlendModeName(Params.Integers[0]);
  end;

  function PlaySE: String;
  var
    SE: TJSONObject;
  begin
    SE := Params.Objects[0];
    PlaySE := rsPlaySe.Format([SE.Strings['name'], SE.Integers['volume'], SE.Integers['pitch'], SE.Integers['pan']]);
  end;

begin
  if (MoveCommand <> nil) and MoveCommand.Find('code', CodeJson) then
    Code := CodeJson.AsInteger;
  if (MoveCommand <> nil) then
    MoveCommand.Find('parameters', Params);

  case Code of
    1: Result := rsMoveDown;
    2: Result := rsMoveLeft;
    3: Result := rsMoveRight;
    4: Result := rsMoveUp;
    5: Result := rsMoveLowerLeft;
    6: Result := rsMoveLowerRight;
    7: Result := rsMoveUpperLeft;
    8: Result := rsMoveUpperRight;
    9: Result := rsMoveAtRandom;
    10: Result := rsMoveTowardsPlayer;
    11: Result := rsMoveAwayFromPlayer;
    12: Result := rs1StepForward;
    13: Result := rs1StepBackward;
    14: Result := rsJump.Format([ParamWithSign(0), ParamWithSign(1)]);
    15: Result := rsWait.Format([Params.Integers[0]]);

    16: Result := rsTurnDown;
    17: Result := rsTurnLeft;
    18: Result := rsTurnRight;
    19: Result := rsTurnUp;

    20: Result := rsTurn90DegreesRight;
    21: Result := rsTurn90DegreesLeft;
    22: Result := rsTurn180Degrees;
    23: Result := rsTurn90DegreesRightOrLeft;

    24: Result := rsTurnAtRandom;
    25: Result := rsTurnTowardsPlayer;
    26: Result := rsTurnAwayFromPlayer;
    27: Result := rsSwitchOn.Format([Db.GetSwitchName(Params.Integers[0])]);
    28: Result := rsSwitchOff.Format([Db.GetSwitchName(Params.Integers[0])]);
    29: Result := rsSpeed.Format([Params.Integers[0]]);
    30: Result := rsFrequency.Format([Params.Integers[0]]);

    31: Result := rsWalkingAnimationOn;
    32: Result := rsWalkingAnimationOff;
    33: Result := rsSteppingAnimationOn;
    34: Result := rsSteppingAnimationOff;
    35: Result := rsDirectionFixOn;
    36: Result := rsDirectionFixOff;
    37: Result := rsThroughOn;
    38: Result := rsThroughOff;
    39: Result := rsTransparentOn;
    40: Result := rsTransparentOff;
    41: Result := Image;
    42: Result := rsOpacity.Format([Params.Integers[0]]);
    43: Result := rsBlendMode.Format([BlendMode]);
    44: Result := PlaySE;
    45: Result := rsScript.Format([Params.Strings[0]]);
  else
    Result := rsUnknownParameter;
  end;
end;

end.

